<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div,
        p {
            width: 300px;
            height: 30px;
            line-height: 30px;
            color: #fff;
            background-color: pink;
        }
    </style>
</head>

<body>
    <button>显示系统当前时间</button>
    <div>某个时间</div>
    <p>123</p>
    <script>
        //当我们点击按钮，div文字会发生变化
        //1，获取元素
        var btn = document.querySelector('button');
        var div = document.querySelector('div');
        //2，注册事件
        btn.onclick = function() {
            // div.innerText = '2022-4-29';
            div.innerText = getDaty();
        }

        function getDaty() {
            //格式化日期年月日
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var dates = date.getDate();
            var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
            var day = date.getDay();
            return '今天是' + year + '年' + month + '月' + dates + '日' + arr[day];
        }
        //我们元素可以不添加事件
        var p = document.querySelector('p');
        p.innerText = getDaty();
    </script>

</body>

</html>